package com.tbit.uqbike.client.util.H5Login;

import cn.hutool.crypto.asymmetric.SignAlgorithm;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:24 2023/10/17
 */
public class H5GenSignUtil {

    static boolean hexStr = false;

    /**
     * token校验
     *
     * @param appId
     * @param traceId
     * @param timestamp
     * @param token
     * @param version
     * @return
     */
    public static String testGenSign(String appId, String traceId, String timestamp, String token, String version, String privateKey
            , String publicKey) {

        String data = appId + traceId + timestamp + token + version;
        System.out.println("待签名数据：" + data);
        LySign lySign = LySign.of(SignAlgorithm.SHA256withRSA, privateKey, publicKey, hexStr);
        String sign = lySign.sign(data);
        System.out.println("签名数据：" + sign);
        System.out.println("验证结果:" + lySign.verify(data, sign));
        return sign;
    }

    /**
     * 手机号解密
     *
     * @param msisdn
     * @param key
     * @return
     */
    public static String testAesDecode(String msisdn, String key) {
        AES aes = new AES(getAesKey(key));
        return new String(aes.decryptBase64(msisdn));

    }

    public static String getAesKey(String key) {
        if (StringUtils.isNotBlank(key)) {
            if (key.length() > 16) {
                return key.substring(0, 16);
            }
            return StringUtils.rightPad(key, 16, "0");
        }
        return StringUtils.EMPTY;
    }

}
